package com.swpu.zdl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.swpu.zdl.mapper.StudentMapper;
import com.swpu.zdl.pojo.Student;
import com.swpu.zdl.service.StudentService;
import com.swpu.zdl.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    // 查询所有学生的信息
    @GetMapping("/getAll")
   public Result getAll(){
        List<Student> students = studentService.getAll();
        return new Result(200, "全部学生信息", students);
    }
    // 添加学生
    @PostMapping("/addStudent")
    public Result  addStudent(@RequestBody Student student){
        boolean save = studentService.addStudent(student);
        if (!save){
            return new Result(500, "添加失败");
        }
        return new Result(200, "添加成功");
    }
    // 删除学生
    @DeleteMapping("/deleteStudent/{sno}")
    public Result deleteStudent(@PathVariable String sno){
        boolean b = studentService.deleteStudent(sno);
        if (!b){
            return new Result(500,"删除失败");
        }
        return new Result(200, "删除成功");
    }

    // 查询所有学生的选课信息
    @GetMapping("/getAllMsg")
    public Result getAllMsg(){
        List<Student> students = studentService.getAllMsg();
        return new Result(200, "所有学生选课信息", students);
    }
    // 修改学生信息
    @PutMapping("/updateStudent/{sno}")
    public Result updateStudent(@PathVariable String sno, @RequestBody(required = false) Student student){
        Boolean b = studentService.updateStudentMsg(sno, student);
        return new Result(200, "修改学生信息成功");
    }
    // 按学号查询单个学生的选课信息
    @GetMapping("/getMsgBySno")
    public Result getMsgBySno(String sno){
        Student student = studentService.getMsgBySno(sno);
        return new Result(200, "单个学生选课信息", student);
    }
    // 按姓名模糊查询学生及其选课信息
    @GetMapping("/getByName")
    public Result getByName(String name){
        List<Student> students = studentService.getByName(name);
        return new Result(200, "按姓名模糊查询学生姓名", students);
    }
}
